<?php

namespace App\Console\Commands\OpenShop;

use App\Enums\OpenShop\InvestmentPromotion\SignOrderEnum;
use App\Enums\OpenShop\MissionTemplateEnum;
use App\Library\Rms\RmsUserApi;
use App\Models\OpenShop\Common\OpenShopOrderManagerModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopMainProcessModel;
use App\Service\OpenShop\Feishu\SpaceDesignService;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Console\Command;

class InitFinishDesignOrderCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'openShop:init-finish-design-order';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '开店 - 初始化创建设计订单';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @throws GuzzleException
     */
    public function handle()
    {
        OpenShopMainProcessModel
            ::query()
            ->with('signOrderRel')
            ->where('nowStepStatus', MissionTemplateEnum::STEP_STATUS_13)
            ->get()
            ->each(function ($item) {

                if (!$item->signOrderRel) {
                    return;
                }

                if ($item->signOrderRel->status == SignOrderEnum::ORDER_STATUS_NO) {
                    return;
                }

                $userId = OpenShopOrderManagerModel::getManagerUid($item->id, OpenShopOrderManagerModel::MANAGER_TYPE_3);
                $userInfo = (new RmsUserApi())->getUserInfoByIds([$userId])['data'][0] ?? [];

                request()->replace(['userInfo' => (object)$userInfo]);

                // 自动生成设计师订单
                SpaceDesignService::insertSpaceDesignCostData(
                    $item->orderNo,
                    600000,
                    150000,
                    0,
                    0,
                    0
                );

                dump($item->orderNo . '-'. $item->shopId);
            });
    }
}
